// mircoApp/cloud_shop/cloud_shop_rank/cloud_shop_rank.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: "0",
    first_category_list: [],
    page: 1,
    total_page: 0,
    isLoadMore: true,
    dataList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  toDetail(e) {
    console.log(e)
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + id,
    });
  },
  tabChange(e) {
    this.setData({
      active: e.detail.name,
    });
    this.init();
    this.getList();
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      dataList: [],
    });
  },
  async getList() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(
        "plugin.cloud-shop.frontend.day-sales-rank.goodsList"
      ),
      data: {
        first_category_id: this.data.active,
      },
    });
    if (data.result) {
      this.setData({
        dataList: data.data.list.data,
        total_page: data.data.list.last_page,
        isLoadMore: true,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getMoreData() {
    console.log('dddddddd', this.data.page, this.data.total_page)
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss(
          "plugin.cloud-shop.frontend.day-sales-rank.goodsList"
        ),
        data: {
          first_category_id: this.data.active,
          page: this.data.page,
        },
      });
      if (data.result) {
        this.setData({
          dataList: this.data.dataList.concat(data.data.list.data),
          isLoadMore: true
        });
      } else {
        this.setData({
          page: this.data.page - 1,
        });
        app.tips(data.msg);
      }
    }
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(
        "plugin.cloud-shop.frontend.day-sales-rank.index"
      ),
    });
    if (data.result) {
      if (data.data.first_category_list.length > 0) {
        this.data.active = data.data.first_category_list[0].id;
        this.setData({
          first_category_list: data.data.first_category_list,
          active: this.data.active,
        });
        this.init();
        this.getList();
        let tab_component = this.selectComponent('.tab_component');
        console.log(tab_component)
        tab_component.resize();
      }
    } else {
      app.tips(data.msg);
    }
  },
});